package com.example.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.school.dto.AdminGetListDto;
import com.example.school.mapper.UserMapper;
import com.example.school.pojo.Orders;
import com.example.school.pojo.User;
import com.example.school.service.OrdersService;
import com.example.school.mapper.OrdersMapper;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.PageResponseResult;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 24354
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2024-03-16 19:11:43
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getOrdersList(AdminGetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        Page<Orders> page=new Page(dto.getPage(),dto.getSize());
        Page<Orders> ordersPage = ordersMapper.getAll(page, dto);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) ordersPage.getTotal());
        result.setData(ordersPage.getRecords());
        return result;
    }

    @Override
    public Result getUserOrders() {

        Long userId = BaseContext.getCurrentId();
        User checkUser = userMapper.selectById(userId);
        if (userId==null||checkUser==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        List<Orders> ordersList = ordersMapper.selectList(Wrappers.<Orders>lambdaQuery()
                .eq(Orders::getOrdersUserId, userId));
        return Result.okResult(ordersList);

    }
}




